/*
Author: Maciej Oczko
Date: December 2011
*/
#include "DXTexture.h"

DXTexture::~DXTexture()
{
	if(!IsEmpty()) Unload();
}

bool DXTexture::Load(std::wstring p_FilePath)
{
	Resource::Load(p_FilePath);
	m_Identifier = p_FilePath;
	return (m_ref_count == 0) ? Load() : true;
}

bool DXTexture::Load()
{
#ifdef DEBUG
	TRACEW("Texture " << std::wstring(m_Identifier.begin() + m_Identifier.find_last_of('\\') + 1, m_Identifier.end()) << " loading...");
#endif

	IDirect3DDevice9 *device = DXUTGetD3D9Device();
	HRESULT result; 
	m_IsLoaded = SUCCEEDED(result = D3DXCreateTextureFromFile(device, m_Identifier.c_str(), &m_pTexture));

#ifdef DEBUG
	switch(result)
	{
	case D3D_OK: 
		TRACE("Result: D3D_OK");
		break;
	case D3DERR_NOTAVAILABLE:
		TRACE("Result: D3DERR_NOTAVAILABLE");
		break;
	case D3DERR_OUTOFVIDEOMEMORY:
		TRACE("Result: D3DERR_OUTOFVIDEOMEMORY");
		break;
	case D3DERR_INVALIDCALL:
		TRACE("Result: D3DERR_INVALIDCALL");
		break;
	case D3DXERR_INVALIDDATA:
		TRACE("Result: D3DXERR_INVALIDDATA");
		break;
	case E_OUTOFMEMORY:
		TRACE("Result: E_OUTOFMEMORY");
		break;
	default:
		TRACE("Result: Other error");
		break;
	}
#endif

	return m_IsLoaded;
}

void DXTexture::Unload()
{
	Texture::Unload();
	if(m_IsLoaded && m_ref_count == 0)
	{
		m_pTexture->Release();
		m_IsLoaded = false;
#ifdef DEBUG
		TRACEW("Texture " << std::wstring(m_Identifier.begin() + m_Identifier.find_last_of('\\') + 1, m_Identifier.end()) << " unloaded");
#endif
	}
}

bool DXTexture::Reload()
{
	return m_IsLoaded ? true : Load();
}

size_t DXTexture::GetSize()
{
	DWORD sizeOfData = 0;
	//REFGUID refguid = GUID();
	//m_pTexture->GetPrivateData(refguid, 0, &sizeOfData);
	return sizeOfData;
}
